package com.way.filemanager.fragment;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.way.filemanager.R;
import com.way.filemanager.fragment.FileList.IconDrawableManager;
import com.way.filemanager.preferences.FileManagerSettings;
import com.way.filemanager.preferences.Preferences;
import com.way.filemanager.providers.FilelistManager;
import com.way.filemanager.util.AndroidHelper;
import com.way.filemanager.util.CommonFunc;
import com.way.filemanager.util.CommonIdDef;
import com.way.filemanager.util.FileInfo;

import hwdroid.widget.item.DrawableText2Item;
import hwdroid.widget.item.Item.Type;
import hwdroid.widget.itemview.DrawableText2ItemView;

public class CategorySubtypeListAdapter extends BaseAdapter {

    Activity mActivity;
    FilelistManager mFileListManager;
    //public static final int SubTypeConstItemNum = 1;
    int mFileType;
    int mAdapterType;
    IconDrawableManager mIconDrawableManager;
    public CategorySubtypeListAdapter(Activity main,FilelistManager m,int type,IconDrawableManager h)
    {
        mActivity = main;
        mFileListManager = m;
        mFileType =type;
        mIconDrawableManager =h;
    }
    public void ChargeFilelist(FilelistManager m)
    {
        mFileListManager = m;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        int AddItemNum = 0;
        switch(mAdapterType)
        {
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL:
                AddItemNum = 0;
                break;
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCH_BTN:
                AddItemNum = 1;
                break;
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING:
                return mFileListManager.getSearchList().size();
            default:
                AddItemNum = 0;
                break;
        }
        if(mFileListManager != null)
            return mFileListManager.GetWorkingFileCount() + AddItemNum;
        else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }
    public void SetAdapterMode(int type)
    {
        mAdapterType = type;
    }
    public int GetAdapterMode()
    {
        return mAdapterType;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    class ViewHolder {
        ImageView icon;
        TextView filename;
        TextView createTime;
        CheckBox checkbox;
    }
    //TextView mSearchButton;
    ImageView mSearchButton;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int mItemPositon;
        int AddItemNum = 0;
        switch(mAdapterType)
        {
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_NORMAL:
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING:
                break;
            case CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCH_BTN:
                if(position == 0)
                {
                    if(mSearchButton == null)
                    {
                        mSearchButton = new ImageView(mActivity);
                        mSearchButton.setImageResource(R.drawable.hw_ic_search_normal);
                        mSearchButton.setScaleType(ScaleType.CENTER);
                        mSearchButton.setMinimumHeight((int)AndroidHelper.convertDpToPixel(mActivity, 50));
                    }
                    convertView = mSearchButton;
                    convertView.setId(CommonIdDef.SEARCH_ITEM_POSITON);
                    return mSearchButton;
                }
                AddItemNum = 1;
                break;
            default:
                break;
        }


        mItemPositon = position - AddItemNum;
        if((convertView != null)&&(convertView.getId() == CommonIdDef.SEARCH_ITEM_POSITON))
        {
            convertView = null;
        }

        
        FileInfo fileInfo = null;
        if(mAdapterType == CommonIdDef.CATEGORY_SUBTYPE_MODE_SEARCHING)
        {
            fileInfo = mFileListManager.getSearchList().get(mItemPositon);
        }else if(mAdapterType == CommonIdDef.CATEGORY_SUBTYPE_MODE_SELECTING)
        {
            fileInfo = mFileListManager.getSelectList().get(mItemPositon);
        }
        else
        {
            fileInfo = mFileListManager.getCurrentFileInfo(mItemPositon);
        }
        if(fileInfo != null)
        {
            String filename = fileInfo.GetFileName();
            DrawableText2Item item = new DrawableText2Item(filename);
            //item.mText = filename;
            item.mSubText = CommonFunc.FormetFileSize(fileInfo.GetFileSize())+ "|" + CommonFunc.CreateTimeFormat(fileInfo.GetModifyTime());
            item.leftDrawable = mIconDrawableManager.getIcon(fileInfo.GetFullPath());//mActivity.getResources().getDrawable(R.drawable.fso_type_document);
            if(mAdapterType == CommonIdDef.CATEGORY_SUBTYPE_MODE_SELECTING)
                item.setTypeMode(Type.CHECK_MODE);
            else
                item.setTypeMode(Type.NORMAL_MODE);
            item.setChecked(fileInfo.mSelected);
            
            if (convertView == null) {
                DrawableText2ItemView itemview = (DrawableText2ItemView)item.newView(mActivity, null);
                itemview.prepareItemView();
                convertView = itemview;
            } else {
                Log.v("tag", "positon " + mItemPositon + " convertView is not null, "  + convertView);
            }
            
            ((DrawableText2ItemView)convertView).setObject(item);
            if(filename.startsWith("."))
            {
                Preferences.getSharedPreferences();
                FileManagerSettings showHiddenPref = FileManagerSettings.SETTINGS_SHOW_HIDDEN;
                if(Preferences.getSharedPreferences().getBoolean(showHiddenPref.getId(),
                         ((Boolean) showHiddenPref.getDefaultValue()).booleanValue()))
                {
                    convertView.setAlpha((float) 0.5);
                }
            }
            else
                convertView.setAlpha((float) 1.0);
        }

        convertView.setId(mItemPositon);
        return convertView;
    }


}
